// JavaScript Document
/*
	Author : tunn
	Created : 3 Feb 2009
	Use : 
		var popup = new Popup('popup1 popup2 popup3 popup4 popup5 popup6');
		- popup1 to popup6 is name of popup 
		- popup.open(name_of_popup); //display popup on screen
		- popup.close(name_of_popup); //hide popup		
*/
var Popup = Class.create();
Popup.prototype = {	
	bg: '',
	windows: {},
	windowsOpen: null,
	current_windows: {},
	count: 0,
	current: -1,
	z_index: 1000,
	active_window: null,
	curIndex: 0,
	getWindow: function(name)
	{
		for(var i=0;i<this.count;i++)
		{
			if(this.windows[i].id == name)
			{
				return this.windows[i];
			}
		}
		return null;
	},
	disableAll: function()
	{
		this.curIndex = this.bg.style.zIndex;
		var cw = this.getWindow(this.active_window);
		this.bg.style.display = 'block';
		if(cw!=null)
			this.bg.style.zIndex = cw.style.zIndex + 2;
			
		//alert('adas');	
	},
	enableAll: function()
	{
		var cw = this.getWindow(this.active_window);
		if(cw==null)
			this.bg.style.display = 'none';
		else
			this.bg.style.zIndex = this.curIndex ;			
	},
	getDocumentHeight: function()
	{		
		var D = document;
		return Math.max(
			Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
			Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
			Math.max(D.body.clientHeight, D.documentElement.clientHeight)
		);
	},
	getDocumentWidth: function()
	{		
		var D = document;
		return Math.max(
			Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
			Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
			Math.max(D.body.clientWidth, D.documentElement.clientWidth)
		);
	},
	pop:function()
	{
		var e = this.windowsOpen.item(this.windowsOpen.selectedIndex);
		this.windowsOpen.remove(e);
	},
	push:function(name)
	{
		var e = document.createElement('option');
		e.text=name;
		this.windowsOpen.appendChild(e);
	},
	center:function(w)
	{
		var parentW = screen.width;
		var parentH = screen.height;
		var childW = w.style.width.substr(0,w.style.width.length-2);
		var childH = w.style.height.substr(0,w.style.height.length-2);
		
		//var left	= (parentW - childW)/2;
		var left=300;
		var top = 0;
		childH = 200;
		if(childH>parentH)
			top = 50;
		else
			top = (parentH - childH)/2;
			//alert(parentW + "\n" +childW);	
		w.style.left = left + 'px';
		w.style.top = 1500 + 'px';	
	},
	initialize: function(w) 
	{
		var ws = w.split(' ');
		var len = ws.length;
		var index=0;
		this.windowsOpen = document.createElement('select');
		
		for(var i=0;i<len;i++)
		{
			if($(ws[i]))
			{
				this.windows[index] = $(ws[i]);
				this.windows[index].style.display = 'none';
				this.windows[index].style.position = 'absolute';
				this.center(this.windows[index]);
				new Draggable(this.windows[index], { handle: this.windows[index] });
				index++;
			}	
			else
			{
				//alert('Window with name "' + '" is not exists!');
			}
		}
		this.count = index;
		//var bg_id = this.bb();
		this.bg = document.createElement('div');
		
		this.bg.className = 'bgWindowPopup';
		var h=this.getDocumentHeight();//document.body.clientHeight;
		this.bg.style.height=h+'px';
		this.bg.style.width = this.getDocumentWidth() + 'px';
		this.bg.style.zIndex = this.z_index;
		//this.bg.appendChild(document.createTextNode(i));
		document.body.appendChild(this.bg);	
		//alert(this.bg);
		//document.body.appendChild(this.windowsOpen);
	},
	open: function(name, left, top)
	{
		if($(name).style.display == 'block')
			return;
		this.active_window = name;
		for(var i=0;i<this.count;i++)
		{
			if(this.windows[i].id == name)
			{
				//alert(this.bg.style.display);
				
				this.bg.style.display='block';
					
				this.current++;
				this.push(name);
				this.bg.style.zIndex=this.z_index+(this.current+1);
				this.windows[i].style.zIndex=this.z_index+(this.current+2);

				this.center(this.windows[i]);
				
				if(typeof(left)!='undefined')
					if(left!=-1)
						this.windows[i].style.left = left+'px';
				if(typeof(top)!='undefined')
					if(top!=-1)
						this.windows[i].style.top = top+'px';					

				this.windows[i].style.display = 'block';
				new Draggable(this.windows[i], { handle: this.windows[i] });
				//alert(this.windows[i]);
				return;
			}
		}
	},
	close:function(wname)
	{
		var name = wname;
		if(typeof(name)=='undefined')
			name = this.active_window;
		for(var i=0;i<this.count;i++)
		{
			if(this.windows[i].id == name)
			{				
				this.windows[i].style.display = 'none';
				this.current--;
				this.pop();
				if(i>0)
					this.active_window = this.windows[i-1].id;
				//alert(this.bg.style.display)
				if(this.windowsOpen.length==0)
					this.bg.style.display='';
				else
					this.bg.style.zIndex=this.windows[i].style.zIndex-2;
				return;
			}
		}
		//alert(this.bg);
	}  
}